package com.central.scheduler.api.controller;

import com.central.common.annotation.LoginUser;
import com.central.common.model.*;
import com.central.scheduler.api.service.ITaskDefinitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * @author Tindy
 * @date 2021/6/24
 * @describe
 */
@RestController
@Api(tags = "task模块api")
@Slf4j
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private ITaskDefinitionService taskDefinedService;

    @ApiOperation(value = "作业任务发布至调度中心")
    @PostMapping("/pv/release")
    public Result release(@RequestBody TaskDefinition taskDefinition) {
        try {
            taskDefinedService.release(taskDefinition);
            return Result.succeed("发布成功");
        }catch (Exception e){
            return Result.failed("发布失败："+e.getMessage());
        }
    }

    @ApiOperation(value = "查询已发布的task列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer",example = "1"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer",example = "10"),
            @ApiImplicitParam(name = "isTmp", value = "是否为临时", required = true, dataType = "Boolean",example = "false")
    })
    @GetMapping("/findTasks")
    public PageResult<TaskDefinition> findTasks(@ApiIgnore @LoginUser SysUser user, @RequestParam Map<String, Object> params) {
        return taskDefinedService.findTasks(user.getCurrentProjectId(),params);
    }

    @ApiOperation(value = "删除已发布的作业")
    @DeleteMapping("/delTask")
    public Result delTask(@ApiIgnore @LoginUser SysUser user,Long id){
        return taskDefinedService.delTask(user,id);
    }


    @ApiOperation(value = "临时执行作业")
    @PostMapping("/pv/tmpRunTask")
    public Result tmpRunTask(@ApiIgnore @LoginUser SysUser user,@RequestBody TaskDefinition taskDefinition) {
        try {
            return taskDefinedService.tmpRunTask(user,taskDefinition);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("执行失败："+e.getLocalizedMessage());
        }
    }

    @ApiOperation(value = "查询作业版本是否已发布")
    @GetMapping("/pv/taskIsRelease")
    public Long taskIsRelease(Long taskDefinedId,Long taskDefinedLogId){
        return taskDefinedService.taskIsRelease(taskDefinedId,taskDefinedLogId);
    }
}
